﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tomahawk.Runtime.Canvas.Sprite;
using System.Xml.Serialization;
using Tomahawk.Runtime.Logic.UI.Components;

namespace Tomahawk.Runtime.Logic.UI
{
    /// <summary>
    /// This controls shows an image on screen.
    /// </summary>
    public class XUIImage : XUIPlaceableControl
    {

        #region "Serializable Properties"

        [XmlElement("Image")]
        public ImageComponent Image;

        #endregion

        public XUIImage()
        {
            this.Image.ResourcePool = Tomahawk.Runtime.Resources.ResourceManager.ResourcePool.GameState;
        }

        /// <summary>
        /// Control Initialization
        /// </summary>
        protected override void OnInitialize()
        {
            base.OnInitialize();

            //hack (minimu depth for image controls is 0.1f)
            if (this.Position.Depth == 0.0f) this.Position.Depth = 0.1f;

            //
            // image component
            //

            this.Image.Initialize(this.ParentUIScene.CanvasLayer);

            //copy position and size to sprites
            this.passPositionToSprites();

        }

        #region "Visibility"

        /// <summary>
        /// Controls visibility changes
        /// </summary>
        protected override void OnChangeVisibility(bool newValue)
        {
            base.OnChangeVisibility(newValue);

            this.Image.SetVisibility(newValue);
        }

        #endregion

        #region "Rendering"
        /// <summary>
        /// Prepares the control for rendering
        /// </summary>
        protected override void OnPrepareSpritesForRendering()
        {
            base.OnPrepareSpritesForRendering();
            this.passPositionToSprites();
        }
        #endregion

        #region "Positioning"

        /// <summary>
        /// Handle a position change
        /// </summary>
        protected override void OnPositionChange()
        {
            base.OnPositionChange();
        }

        /// <summary>
        /// Copy the control position to the sprites
        /// </summary>
        private void passPositionToSprites()
        {
            this.Image.SetPosition(ref this.renderRect);
            this.Image.SetRotation(this.renderRot, ref this.renderOrigin);
            this.Image.SetDepth(this.Position.Depth);
        }

        #endregion

        #region "Focus handling"

        /// <summary>
        /// Focus
        /// </summary>
        protected override void OnFocus()
        {
            base.OnFocus();
        }

        /// <summary>
        /// Focus
        /// </summary>
        protected override void OnUnfocus()
        {
            base.OnUnfocus();
        }

        #endregion

        #region "Update"

        /// <summary>
        /// Control update. 
        /// </summary>
        protected override void OnUpdate(float elapsedTime)
        {
            base.OnUpdate(elapsedTime);
        }

        #endregion

        #region "Destruction"

        /// <summary>
        /// Destruction handling
        /// </summary>
        public override void OnBeforeDestroy()
        {
            base.OnBeforeDestroy();

            this.Image.Destroy();
        }

        #endregion

        #region "Readyness"

        /// <summary>
        /// Returns true when all the resource dependant objects are ready
        /// </summary>
        protected override bool checkResourcesReady()
        {
            return base.checkResourcesReady() &&
                this.Image.IsReady();
        }

        #endregion

        /// <summary>
        /// Gets or sets the alpha of the image component
        /// </summary>
        protected override void OnAlphaChange(byte newAlpha)
        {
            base.OnAlphaChange(newAlpha);
            this.Image.Alpha = newAlpha;
        }

        /// <summary>
        /// Changes the color of the sprite
        /// </summary>
        public void SetColor(ref XStyleManager.FontColor fontColor)
        {
            this.Image.SetColor(ref fontColor);
        }

    }

}
